﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StuManModels;
using StuManDAL;

namespace StudentManageWinForm
{
    public partial class FrmScoreManage : Form
    {
        public FrmScoreManage()
        {
            InitializeComponent();
            Init();
        }
        #region 字段和属性
        private ClassService _classService;
        private ScoreService _scoreService;
        private SubjectService _subjectService;
        private BindingSource _bindingSource;
        private DataSet _dataSet;
        #endregion
        #region 业务逻辑
        private void QueryAllScore()
        {
            //DataSet dataSet = _scoreService.GetAllScore(out string info);
            //if (dataSet==null)
            //{
            //    lbInfo.Text = info;
            //    return;
            //}
            //_bindingSource.DataSource = dataSet.Tables[0];
            _dataSet.Tables[2].DefaultView.RowFilter = string.Empty;
        }
        private void QueryByClass()
        {
            //DataSet dataSet = _scoreService.GetStuScoByClass(Convert.ToInt32(cmbClass.SelectedValue), out string info);
            //if (dataSet==null)
            //{
            //    lbInfo.Text = info;
            //    return;
            //}
            //_bindingSource.DataSource = dataSet.Tables[0];
            _dataSet.Tables[2].DefaultView.RowFilter = $"ClassName='{cmbClass.Text}'";
        }
        private void QueryBySujectAndValue()
        {
            //DataSet dataSet = _scoreService.GetScoreAndOrderBy(Convert.ToInt32(cmbSubjects.SelectedValue), Convert.ToDouble(numScoreValue.Value), out string info);
            //if (dataSet==null)
            //{
            //    lbInfo.Text = info;
            //    return;
            //}
            //_bindingSource.DataSource = dataSet.Tables[0];
            _dataSet.Tables[2].DefaultView.RowFilter = $"SubjectName='{cmbSubjects.Text}' and Value>{numScoreValue.Value}";
            _dataSet.Tables[2].DefaultView.Sort = "Value DESC";
        }
        /// <summary>
        /// 初始化方法
        /// </summary>
        private void InitB()
        {
            _classService = new ClassService();
            _subjectService = new SubjectService();
            _scoreService = new ScoreService();
            DataSet dataSet = _classService.GetAllClassForDataSet(out string infoOfClass);
            if (dataSet != null)
            {
                cmbClass.DataSource = dataSet.Tables[0];
                cmbClass.DisplayMember = "ClassName";
                cmbClass.ValueMember = "ClassId";
            }
            else lbInfo.Text = infoOfClass;
            List<StudentSubject> subjects = _subjectService.GetAllSubjects(out string infoOfSubject);
            if (subjects != null)
            {
                cmbSubjects.DataSource = subjects;
                cmbSubjects.DisplayMember = "SubjectName";
                cmbSubjects.ValueMember = "SubjectId";
            }
            else lbInfo.Text = infoOfSubject;
            _bindingSource = new BindingSource();
            dgvStudentInfo.AutoGenerateColumns = false;
            dgvStudentInfo.DataSource = _bindingSource;
            cmbClass.SelectedIndex = -1;
            cmbSubjects.SelectedIndex = -1;
        }
        private void Init()
        {
            _scoreService = new ScoreService();
            _dataSet = _scoreService.GetAllInfo(out string info);
            if (_dataSet==null)
            {
                lbInfo.Text = info;
                return;
            }
            cmbClass.DataSource = _dataSet.Tables[0];
            cmbClass.DisplayMember = "ClassName";
            cmbClass.ValueMember = "ClassId";
            cmbSubjects.DataSource = _dataSet.Tables[1];
            cmbSubjects.DisplayMember = "SubjectName";
            cmbSubjects.ValueMember = "SubjectId";
            _bindingSource = new BindingSource();
            _bindingSource.DataSource = _dataSet.Tables[2];
            dgvStudentInfo.AutoGenerateColumns = false;
            dgvStudentInfo.DataSource = _bindingSource;
            cmbClass.SelectedIndex = -1;
            cmbSubjects.SelectedIndex = -1;
        }
        #endregion
        private void cmbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            QueryByClass();
            cmbSubjects.SelectedIndex = -1;
            numScoreValue.Value = 0;
        }

        private void numScoreValue_ValueChanged(object sender, EventArgs e)
        {
            QueryBySujectAndValue();
            cmbClass.SelectedIndex = -1;
        }

        private void cmbClass_DropDownClosed(object sender, EventArgs e)
        {
            QueryByClass();
            cmbSubjects.SelectedIndex = -1;
            numScoreValue.Value = 0;
        }

        private void btnGetAllScore_Click(object sender, EventArgs e)
        {
            QueryAllScore();
            cmbClass.SelectedIndex = -1;
            cmbSubjects.SelectedIndex = -1;
        }
    }
}
